package com.steakliu.sun.disuse.api;

import io.netty.channel.Channel;

import java.util.Objects;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 功能说明： 连接信息
 * <p>
 * Original @Author: steakliu-刘牌, 2023-02-05  19:46
 * <p>
 * Copyright (C)2020-2022  steakliu All rights reserved.
 */
public class ConnectionsInfo {
    /**
     * 连接channel
     */
    private Channel channel;
    /**
     * 建立连接的时间
     */
    private long connectionTime;
    /**
     * 上次使用的时间
     */
    private long lastUseTime;
    /**
     * 使用次数
     */
    private AtomicInteger useCount = new AtomicInteger(0);

    public ConnectionsInfo(){}

    public ConnectionsInfo(Channel channel){
        this.channel = channel;
        long currentTimeStamp = System.currentTimeMillis();
        this.connectionTime = currentTimeStamp;
        this.lastUseTime = currentTimeStamp;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }
        ConnectionsInfo that = (ConnectionsInfo) o;
        return Objects.equals(channel, that.channel);
    }

    @Override
    public int hashCode() {
        return Objects.hash(channel);
    }

    public int getUseCount(){
       return useCount.get();
    }

    public int incrementAndGet(){
        return useCount.incrementAndGet();
    }

    public Channel getChannel() {
        return channel;
    }

    public void setChannel(Channel channel) {
        this.channel = channel;
    }

    public long getConnectionTime() {
        return connectionTime;
    }

    public void setConnectionTime(long connectionTime) {
        this.connectionTime = connectionTime;
    }

    public long getLastUseTime() {
        return lastUseTime;
    }

    public void setLastUseTime(long lastUseTime) {
        this.lastUseTime = lastUseTime;
    }

    public void setUseCount(AtomicInteger useCount) {
        this.useCount = useCount;
    }
}
